// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.gameStates.IngameState;
import zombie.iso.Vector2;
import zombie.network.GameServer;
import zombie.util.Type;

public final class LungeState extends State {
    private static final LungeState _instance = new LungeState();
    private final Vector2 temp = new Vector2();
    private static final Integer PARAM_TICK_COUNT = 0;

    public static LungeState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        HashMap hashMap = owner.getStateMachineParams(this);
        if (System.currentTimeMillis() - isoZombie.LungeSoundTime > 5000L) {
            String string = "MaleZombieAttack";
            if (isoZombie.isFemale()) {
                string = "FemaleZombieAttack";
            }

            if (GameServer.bServer) {
                GameServer.sendZombieSound(IsoZombie.ZombieSound.Lunge, isoZombie);
            }

            isoZombie.LungeSoundTime = System.currentTimeMillis();
        }

        isoZombie.LungeTimer = 180.0F;
        hashMap.put(PARAM_TICK_COUNT, IngameState.instance.numberTicks);
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        HashMap hashMap = owner.getStateMachineParams(this);
        owner.setOnFloor(false);
        owner.setShootable(true);
        if (isoZombie.bLunger) {
            isoZombie.walkVariantUse = "ZombieWalk3";
        }

        isoZombie.LungeTimer = isoZombie.LungeTimer - zombie.GameTime.getInstance().getMultiplier() / 1.6F;
        IsoPlayer isoPlayer = Type.tryCastTo(isoZombie.getTarget(), IsoPlayer.class);
        if (isoPlayer != null && isoPlayer.isGhostMode()) {
            isoZombie.LungeTimer = 0.0F;
        }

        if (isoZombie.LungeTimer < 0.0F) {
            isoZombie.LungeTimer = 0.0F;
        }

        if (isoZombie.LungeTimer <= 0.0F) {
            isoZombie.AllowRepathDelay = 0.0F;
        }

        this.temp.x = isoZombie.vectorToTarget.x;
        this.temp.y = isoZombie.vectorToTarget.y;
        isoZombie.getZombieLungeSpeed();
        this.temp.normalize();
        isoZombie.setForwardDirection(this.temp);
        isoZombie.DirectionFromVector(this.temp);
        isoZombie.getVectorFromDirection(isoZombie.getForwardDirection());
        isoZombie.setForwardDirection(this.temp);
        if (!isoZombie.isTargetLocationKnown()
            && isoZombie.LastTargetSeenX != -1
            && !owner.getPathFindBehavior2()
                .isTargetLocation((float)isoZombie.LastTargetSeenX + 0.5F, (float)isoZombie.LastTargetSeenY + 0.5F, (float)isoZombie.LastTargetSeenZ)) {
            isoZombie.LungeTimer = 0.0F;
            owner.pathToLocation(isoZombie.LastTargetSeenX, isoZombie.LastTargetSeenY, isoZombie.LastTargetSeenZ);
        }

        long _long = (Long)hashMap.get(PARAM_TICK_COUNT);
        if (IngameState.instance.numberTicks - _long == 2L) {
            ((IsoZombie)owner).parameterZombieState.setState(ParameterZombieState.State.LockTarget);
        }
    }

    @Override
    public void exit(IsoGameCharacter chr) {
    }

    @Override
    public boolean isMoving(IsoGameCharacter owner) {
        return true;
    }
}
